import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ardilla here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ardilla extends Enemigo
{
    /**
     * Act - do whatever the Ardilla wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean mov;
    private int yVel=0;
    /**
     * Actua
     */
    public void act() 
    {
       // Add your action code here.
        mueve();
    } 
    /**
     * Constructor
     */
    public Ardilla()
    {
        mov=true;
    
    }
    /**
     * Realiza el movimiento de la ardilla sobre las ramas solo si encuentra una rama por eso a veces las ardillas
     * se caen, la variable mov hace que vallan a la izquierda y a la derecha
     */
    public void mueve()
    { 
        GreenfootImage imagen=getImage();
        MoveWorld mundo=(MoveWorld)this.getWorld();
        int height=imagen.getHeight()/2+5;
        Rama rama=(Rama)getOneObjectAtOffset(0,height,Rama.class);
        if(rama!=null)
        {
             yVel=-1;
             if((mov==true&&getOneObjectAtOffset(-5,height+10,Rama.class)==null)|| (mov==false&&getOneObjectAtOffset(5,height+10,Rama.class)==null))
                  yVel=-20;
             setLocation(getX(),rama.getY()-rama.getImage().getHeight()/2-height+8);
        }
        if(mov==false && getOneObjectAtOffset(30,5,Rama.class)==null)
        { 
            setLocation(getX()+1,getY());
        
        }
        else
           if(mov==true&&getOneObjectAtOffset(-30,5,Rama.class)==null)
           {
                 setLocation(getX()-1,getY());
           }
        yVel+=1;
        setLocation(getX(),getY()+yVel);
        if(getX()<170&&mov==true)
        {  
            mov=false;
            this.getImage().mirrorHorizontally();
        }
        else
           if(getX()>680&&mov==false)
           {
               mov=true;
               this.getImage().mirrorHorizontally();
           } 
    }

}
